<template>
  <el-card style="max-width: 1100px; margin: 30px auto;">
    <div style="display: flex; justify-content: space-between; align-items: center;">
      <h1>新增盘盈盘亏入库</h1>
      <el-button @click="goBack">返回</el-button>
    </div>
    <el-divider />
    <el-form
      ref="formRef"
      :model="ruleForm"
      label-width="100px"
      style="margin-top: 20px"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="入库单号" prop="inboundCode">
            <el-input v-model="ruleForm.inboundCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库类型" prop="inboundTypeName">
            <el-select v-model="ruleForm.inboundTypeName" placeholder="请选择入库类型">
              <el-option label="盘盈入库" value="盘盈入库" />
              <el-option label="盘亏入库" value="盘亏入库" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="仓库" prop="depotId">
                        <el-select v-model="ruleForm.depotId" placeholder="请选择仓库" style="width: 100%">
                            <el-option v-for="a in Depot" :key="a.depotId" :label="a.depotName" :value="a.depotId" />
                        </el-select>
                    </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="入库日期" prop="inboundTime">
            <el-date-picker
              v-model="ruleForm.inboundTime"
              type="datetime"
              placeholder="请选择入库日期"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
       <el-col :span="12">
          <el-form-item label="关联单号" prop="inventoryCode">
            <el-input v-model="ruleForm.inventoryCode" />
          </el-form-item>
        </el-col>
      <el-row>
          <el-col :span="12">
          <el-form-item label="经办人" prop="inboundHandler">
            <el-input v-model="ruleForm.inboundHandler"  />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注说明" prop="inboundRemark">
            <el-input
              v-model="ruleForm.inboundRemark"
              type="textarea"
              :rows="4"
              maxlength="500"
              show-word-limit
              placeholder="请输入备注说明"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" style="text-align: left;">
          <el-button type="primary" @click="onSubmit">提交</el-button>
          <el-button @click="">重置</el-button>
          <el-button @click="goBack">取消</el-button>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { http } from '@/axios/axiosHelpers'

onMounted(() => {
    ruleForm.value.inboundCode = generateInboundCode();
    getDepot();
})
// 自动生成入库单号
function generateInboundCode() {
  const now = new Date()
  const pad = (n: number) => n.toString().padStart(2, '0')
  const dateStr = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}`
  const rand = Math.floor(1000 + Math.random() * 9000)
  return `RKDH${dateStr}${rand}`
}
const router = useRouter()
const formRef = ref<FormInstance>()
const ruleForm = ref({
  "inboundCode": "",
  "inboundTypeName": "",
  "depotId": '',
  "inboundTime": "2025-08-06T00:17:42.542Z",
  "inventoryCode": "",
  "inboundHandler": "",
  "inboundRemark": ""
})

const onSubmit = () => {
  formRef.value?.validate((valid) => {
    http.post("/basicwrite/Inbound/AddInbound",ruleForm.value).then(res=>{
       if (valid) {
      // 这里可以提交到后端
      ElMessage.success(res.data.msg)
      // router.push('/your-list-page')
    } else {
      ElMessage.error(res.data.msg)
    }
    })
   
  })
}

// const onReset = () => {
//   formRef.value?.resetFields()
//   form.inboundCode = generateInboundCode()
//   form.inboundDate = new Date()
// }

const goBack = () => {
  router.back()
}

//仓库
const Depot = ref([{
    "depotId": '',
    "depotName": "",
}])
const getDepot = () => {

    console.log(Depot.value);
    http.get("/basicread/Depot/GetDepot").then(res => {
        Depot.value = res.data.data
    })
}
</script>

<style scoped>
h2 {
  font-weight: bold;
  margin-bottom: 0;
}
</style>